package br.com.criativasystem.controle;

import br.com.criativasystem.modelo.Login;
import br.com.criativasystem.visao.ViewLogin;
import java.awt.AWTKeyStroke;
import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JOptionPane;

/**
 *
 * @author Lucas
 */
public class ControleLogin implements ActionListener {

    ViewLogin login;
    DAOLogin logar  = new DAOLogin();
    
    public ControleLogin() {
    }
 
    
    public ControleLogin(ViewLogin login) {
        this.login = login;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        Login l = new Login();
        l.setUsuario(login.getTxtUsuario().getText());
        l.setSenha(login.getTxtSenha().getText());
        
        if(e.getSource() == login.getBtnLogar()){
            Boolean loga = logar.selecionaUsuario(l);         
            if(loga) {
                login.hide();
            } else {
                JOptionPane.showMessageDialog(login, "Usuario ou senha invalida");
                login.getTxtUsuario().setText("");
                login.getTxtUsuario().requestFocus();
                login.getTxtSenha().setText("");
            }           
        } else if (e.getSource() == login.getBtnSair()) {
            System.exit(0);
        }

    }

    public static void considerarEnterComoTab (Component comp) {  
        Set<AWTKeyStroke> keystrokes = comp.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);  
        Set<AWTKeyStroke> newKeystrokes = new HashSet<AWTKeyStroke> (keystrokes);  
        newKeystrokes.add (AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ENTER, 0));  
        comp.setFocusTraversalKeys (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newKeystrokes);   
    }    
}
